<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input v-model="obj.name" type="text" />
    </div>
    <div>
      <span>年龄:</span>
      <input v-model="obj.age" type="number" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="obj.sex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="btn">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(itms, index) in arr" :key="index">
          <td>{{ index + 1 }}</td>
          <td>{{ itms.name }}</td>
          <td>{{ itms.age }}</td>
          <td>{{ itms.sex }}</td>
          <td>
            <button @click="dele(index)">删除</button>
            <button @click="xiu(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      arr: [],
      obj: {},
      fals:false,
      b:0

    };
  },
  methods: {
    btn() {
      if (!(this.obj.name && this.obj.age && this.obj.sex))  return alert("请输入完整");
      if(!this.fals){
      this.arr.push({ ...this.obj });
      this.obj.name = "";
      this.obj.age = "";
      this.obj.sex = "";
      }else{
        this.arr[this.b]['name'] = this.obj.name;
        this.arr[this.b]['age'] = this.obj.age;
        this.arr[this.b]['sex'] = this.obj.sex;
        this.obj.name = "";
      this.obj.age = "";
      this.obj.sex = "";
        this.fals = false
      }
    },
    dele(a) {
      this.arr.splice(a, 1);
    },
    xiu(a) {
      this.b = a
      this.fals=true
      this.obj.name = this.arr[a].name
      this.obj.age = this.arr[a].age
      this.obj.sex = this.arr[a].sex
    },
  },
};
</script>